package com.travel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianfeng.util.ResultMsg;
import com.travel.entity.TbStrstegy;
import com.travel.entity.TbView;
import com.travel.mapper.StrstegyMapper;
import com.travel.mapper.UserMapper;
import com.travel.mapper.ViewMapper;
import com.travel.service.StrstegyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class StrstegyServiceImpl extends ServiceImpl<StrstegyMapper, TbStrstegy> implements StrstegyService {
    @Autowired
    StrstegyMapper strstegyMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ViewMapper viewMapper;

    @Override
    public ResultMsg strstegyGetAll(int current, int size) {
        //分页
        Page<TbStrstegy> pages = new Page<>(current,size);
        Page<TbStrstegy> page = super.page(pages);
        return ResultMsg.success(page);
    }

    @Override
    public ResultMsg strstegyGetOne(Integer strstegyId) {
        QueryWrapper<TbStrstegy> wrapper = new QueryWrapper<>();
        wrapper.eq("strstegy_id",strstegyId);
        TbStrstegy strstegy = super.getOne(wrapper);
        if (strstegy!=null){
            return ResultMsg.success(strstegy);
        }
        return ResultMsg.error(1004,"查询一个攻略失败");
    }

    @Override
    public ResultMsg strstegyGetUserId(Integer userId) {
        QueryWrapper<TbStrstegy> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        Map<String, Object> map = super.getMap(wrapper);
        if (map!=null){
            return ResultMsg.success(map);
        }
        return ResultMsg.error(1004,"根据用户Id查询一个攻略失败");
    }

    @Override
    public ResultMsg strstegyAddOne(TbStrstegy tbStrstegy) {
        boolean save = super.save(tbStrstegy);
        if (save)
            return ResultMsg.success();
        return ResultMsg.error(1004,"添加一个攻略失败");
    }

    @Override
    public ResultMsg strstegyUpdateOne(TbStrstegy tbStrstegy) {
        boolean res = super.saveOrUpdate(tbStrstegy);
        if (res)
            return ResultMsg.success(200);
        return ResultMsg.error(201,"修改失败");
    }

    @Override
    public ResultMsg strstegyDeleteOne(Integer strstegyId) {
        boolean removeById = super.removeById(strstegyId);
        if (removeById)
            return ResultMsg.success();
        return ResultMsg.error(1004,"删除一个攻略失败");
    }

    @Override
    public ResultMsg strstegyView(Integer viewId) {
        QueryWrapper<TbStrstegy> wrapper = new QueryWrapper<>();
        wrapper.eq("view_id",viewId);
        Map<String, Object> map = super.getMap(wrapper);
        if (map!=null){
            return ResultMsg.success(map);
        }
        return ResultMsg.error(1004,"查询攻略失败");
    }

    @Override
    public ResultMsg strstegyViewName(TbView tbView) {
        QueryWrapper<TbStrstegy> wrapper = new QueryWrapper<>();
        String viewName = tbView.getViewName();
        wrapper.eq("view_name",viewName);
        Map<String, Object> map = super.getMap(wrapper);
        if (map!=null) {
            return ResultMsg.success(map);
        }
        return ResultMsg.error(1004,"查询失败");
    }

    @Override
    public ResultMsg strstegySeleteAll() {
        List<TbStrstegy> list = super.list();
        if (list!=null){
            return ResultMsg.success(list);
        }
        return ResultMsg.error(1003,"数据异常");
    }


    @Override
    public ResultMsg findOne(int sid) {
        TbStrstegy res = super.getById(sid);

        return ResultMsg.success(res);
    }
}
